<template>
  <a-row :gutter="10">
    <a-col :md="8" :sm="24">
      <a-card :bordered="false">
        <div style="background: #fff;padding-left:16px;height: 100%; margin-top: 5px">
          <!--<a-input-search @search="onSearch" style="width:100%;margin-top: 10px" placeholder="请输入目录名称"/>-->
          <!-- 树-->
          <template v-if="departTree.length>0">
            <!--组织机构-->
            <a-tree
              showLine
              :load-data="onLoadData"
              :selectedKeys="selectedKeys"
              :checkStrictly="true"
              @select="onSelect"
              :dropdownStyle="{maxHeight:'200px',overflow:'auto'}"
              :treeData="departTree"
              :autoExpandParent="autoExpandParent"
              :expandedKeys="iExpandedKeys"
              @expand="onExpand"
            />

          </template>
          <div style="margin-top: 24px;" v-else-if="departTree.length==0">
            <h3><span>暂无有效目录信息</span></h3>
          </div>
          <div style="margin-top: 24px;" v-else><h3>暂无数据</h3></div>
        </div>
      </a-card>
    </a-col>
    <a-col :md="16" :sm="24">
      <a-card :bordered="false" style="margin: 0">
        <DocHookUpTable ref="DeptUserInfo"
                        :selectedKeys="selectedKeys"
                        :selectedKeysNode="selectedKeysNode"
                        @selExpanded="handelExpanded"
                        @handleReset="clearSelectedDepartKeys">

        </DocHookUpTable>
      </a-card>
    </a-col>
  </a-row>
</template>


<script>

  import DocHookUpTable from './modules/DocHookUpTable'
  import {queryMyDepartTreeList, searchByKeywords} from '@/api/api'
  import {JeecgListMixin} from '@/mixins/JeecgListMixin'
  import { getAction, deleteAction } from '@/api/manage'
  import { getFileDetailsInfoByPid } from '@/api/digitization'



  export default {
    name: 'DocHookUp',
    mixins: [JeecgListMixin],
    components: {
      DocHookUpTable,
    },
    data() {
      return {
        currentDeptId: '',
        iExpandedKeys: [],
        loading: false,
        autoExpandParent: true,
        currFlowId: '',
        currFlowName: '',
        disable: true,
        treeData: [],
        visible: false,
        departTree: [],
        rightClickSelectedKey: '',
        hiding: true,
        model: {},
        dropTrigger: '',
        depart: {},
        disableSubmit: false,
        checkedKeys: [],
        selectedKeys: [],
        selectedKeysNode: {},
        autoIncr: 1,
        currSelected: {},
        form: this.$form.createForm(this),
        labelCol: {
          xs: {span: 24},
          sm: {span: 5}
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16}
        },
        graphDatasource: {
          nodes: [],
          edges: []
        },
        userIdentity:"",
        tabDat:[],

        innerColumns:[
            {
             title: '设备编号',
             dataIndex: 'eqp_no',
             key: 'eqp_no',
            },
            {
              title: '设备名称',
              dataIndex: 'name',
              key: 'name',
            },
            {
              title: '类名',
              dataIndex: 'typeName',
              key: 'typeName',
            },
            {
              title: '备注',
              dataIndex: 'memo',
              key: 'memo',
              ellipsis: true,
            },

          ],
        dataSource:this.getTable(),
        visibleModal: false,
        selectedRowKeys: ['1'],
        loading: false,
        gysWidth: 700,
        currentRow:{},

      }
    },
    methods: {
      handelExpanded(selKeys){
        this.onExpand(selKeys);
        this.onLoadData(this.selectedKeysNode)
      },
      callback(key) {
        //console.log(key)
      },
      loadData() {
        this.refresh();
      },
      clearSelectedDepartKeys() {
        this.selectedKeys = [];
        this.$refs.DeptUserInfo.currentDeptId='';
      },
      loadTree() {
        let that = this
        that.treeData = []
        that.departTree = []

      /**   queryMyDepartTreeList().then((res) => {
          if (res.success && res.result ) {
            for (let i = 0; i < res.result.length; i++) {
              let temp = res.result[i]
              that.treeData.push(temp)
              that.departTree.push(temp)
              that.setThisExpandedKeys(temp)
              // console.log(temp.id)
            }
            this.loading = false
          }
          that.userIdentity = res.message
          console.log(that.departTree)
        })
*/

       getAction("/base/fmTypeDetails/rootList","").then(res=>{
          if (res.success && res.result ) {
          for (let i = 0; i < res.result.records.length; i++) {
              let temp = res.result.records[i]
              temp['title'] = temp.typeName;
              temp['key'] = temp.id;

              if(temp.hasChild == '1'){
                temp['children'] = [{}];
              }

              that.treeData.push(temp)
              that.departTree.push(temp)
             // that.setThisExpandedKeys(temp)
              // console.log(temp.id)
            }
            that.loading = false
          }
        })
      },
      setThisExpandedKeys(node) {
        //只展开一级目录
        if (node.children && node.children.length > 0) {
          this.iExpandedKeys.push(node.key)
          //下方代码放开注释则默认展开所有节点
          /**
           for (let a = 0; a < node.children.length; a++) {
            this.setThisExpandedKeys(node.children[a])
          }
           */
        }
      },
      refresh() {
        this.loading = true
        this.loadTree()
      },

      onExpand(expandedKeys) {
        // console.log('onExpand', expandedKeys)
        // if not set autoExpandParent to false, if children expanded, parent can not collapse.
        // or, you can remove all expanded children keys.
        this.iExpandedKeys = expandedKeys
        this.autoExpandParent = false
      },
      onLoadData(treeNode) {
        return new Promise(resolve => {
          if (treeNode.dataRef.hasChild!='1') {
            resolve();
            return;
          }
          setTimeout(() => {
            getAction("/base/fmTypeDetails/childList?pid="+treeNode.eventKey).then(res=>{
              if (res.success && res.result ) {
                treeNode.dataRef.children = [];
                // debugger
                for (let i = 0; i < res.result.records.length; i++) {
                   let temp = res.result.records[i]
                   temp['title'] = temp.typeName;
                   temp['key'] = temp.id;

                   if(temp.hasChild == '1'){
                     temp['children'] = [];
                   }else{
                     temp['children'] = null;
                   }
                  // treeNode.dataRef.children = [temp];
                  treeNode.dataRef.children.push(temp);
                  this.treeData = [...this.treeData];
                   resolve();
                 }
                console.log(this.treeData,this.departTree)
                }
            });
          }, 100);
        });
      },
      onSearch(value) {
        let that = this
        if (value) {
          searchByKeywords({keyWord: value,myDeptSearch:'1'}).then((res) => {
            if (res.success) {
              that.departTree = []
              for (let i = 0; i < res.result.length; i++) {
                let temp = res.result[i]
                that.departTree.push(temp)
              }
            } else {
              that.$message.warning(res.message)
            }
          })
        } else {
          that.loadTree()
        }

      },
      onSelect(selectedKeys, e) {
        this.selectedKeys = selectedKeys
        this.selectedKeysNode = e.node
        console.log(selectedKeys,e);

      },
      //弹窗相关方法
      getTable(){
        getAction('/base/dtgEquipmentInfo/list').then(res => {
          this.dataSource = res.result.records;
        });
      },
      showModal(){
        this.visibleModal = true;
      },
      handleOk(e) {
        this.visibleModal = false;
      },
      handelCancel(){
        this.handleOk();
      },
      onSelectChange(selectedRowKeys,row){
        debugger
        this.selectedRowKeys = selectedRowKeys;
        this.currentRow = row[0];
        this.$nextTick(() => {
          this.form.setFieldsValue(pick({'supplier':row[0].name},'supplier'))
        })
      },

    },
    created() {
      this.currFlowId = this.$route.params.id
      this.currFlowName = this.$route.params.name


    },

  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>